package com.example.test.dialogfactory;

import static easier.zxing.ZXingManager.decodeQRCode;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultCallback;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.Result;
import easier.app.x.result.pic.TakePhoto;
import easier.app.x.view.BitmapUtils;
import java.io.InputStream;

public class MainActivity108 extends AppCompatActivity {

    private static final String TAG = "MainActivity108";
    private ImageView image0;
    private ImageView image1;
    private ImageView image2;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity108.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main108 );
        initView();
    }

    private void initView() {

        image0 = findViewById( R.id.image0 );
        image1 = findViewById( R.id.image1 );
        image2 = findViewById( R.id.image2 );

        image0.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                TakePhoto.fromGallery( getSupportFragmentManager(), new ActivityResultCallback< Uri >() {
                    @Override
                    public void onActivityResult( Uri result ) {

                        Log.i( TAG, "onActivityResult: " + result );
                        try {
                            InputStream inputStream = v.getContext().getContentResolver().openInputStream( result );
                            Bitmap bitmap = BitmapFactory.decodeStream( inputStream );
                            image0.setImageBitmap( bitmap );
                            com.google.zxing.Result result1 = decodeQRCode( bitmap );
                            Log.i( TAG, "onActivityResult: bitmap:" + result1 );

                            Bitmap toGrayscale = BitmapUtils.toGrayscale( bitmap );
                            image1.setImageBitmap( toGrayscale );
                            Result result2 = decodeQRCode( toGrayscale );
                            Log.i( TAG, "onActivityResult: toGrayscale:" + result2 );

                            Bitmap blackWhite = BitmapUtils.convertToBlackWhite( bitmap );
                            image2.setImageBitmap( blackWhite );
                            Result result3 = decodeQRCode( blackWhite );
                            Log.i( TAG, "onActivityResult: blackWhite:" + result3 );

                        } catch ( Exception e ) {
                            e.printStackTrace();
                        }
                    }
                } );
            }
        } );
    }
}